//
//  YLInvestmentVoucherController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/12/12.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLInvestmentVoucherController.h"
#import "YLInvestmentTableViewCell.h"
#import "YLGlobeHeader.h"
#import "YLInvestmentCertificateModel.h"
#import "YLInvestmentCertificateViewModel.h"
#import <MJRefresh.h>
@interface YLInvestmentVoucherController ()
@property (nonatomic, strong) YLInvestmentCertificateViewModel *viewModel;

@property (nonatomic, assign) NSInteger index;
@end

@implementation YLInvestmentVoucherController

- (YLInvestmentCertificateViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLInvestmentCertificateViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.index = 1;
        [weakSelf loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.index ++;
        [weakSelf loadData];
    }];
    
    // 一开始加载的时候不要显示footer
    self.tableView.mj_footer.hidden = YES;
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    __weak typeof(self) weakSelf = self;
    
    [[self.viewModel.requestCommand execute:@(_index)] subscribeNext:^(id  _Nullable x) {
        weakSelf.tableView.emptyDataSetSource = self;
        weakSelf.tableView.emptyDataSetDelegate = self;
        // 加载成功后结束刷新
        [weakSelf tableViewEndRefreshing];
        
        [weakSelf.tableView reloadData];
        
    }];
    
    [self.viewModel.wh_loadDataErrorSubject subscribeNext:^(id  _Nullable x) {
        weakSelf.tableView.emptyDataSetSource = weakSelf;
        weakSelf.tableView.emptyDataSetDelegate = weakSelf;
        [weakSelf.tableView reloadData];
        [weakSelf tableViewEndRefreshing];
    }];
    
    [self.viewModel.wh_noMoreDataSubject subscribeNext:^(id  _Nullable x) {
        weakSelf.tableView.emptyDataSetSource = weakSelf;
        weakSelf.tableView.emptyDataSetDelegate = weakSelf;
        [weakSelf.tableView reloadData];
        [weakSelf tableViewEndRefreshing];
        [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
    
}

- (void)tableViewEndRefreshing
{
    // 如果没有数据，就不要显示 加载 东西了 否则 丑
    if (self.viewModel.models.count == 0) {
        self.tableView.mj_footer.hidden = YES;
        self.tableView.mj_header.hidden = YES;
    } else {
        self.tableView.mj_footer.hidden = NO;
        self.tableView.mj_header.hidden = NO;
    }
    
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLInvestmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLInvestmentTableViewCell cellID]];
    cell.model = self.viewModel.models[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tabBarController setSelectedIndex:1];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
